<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房源名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入房源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户型" prop="layout">
        <el-select v-model="queryParams.layout" placeholder="请选择户型" clearable>
          <el-option
            v-for="dict in dict.type.sys_property_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="房源状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择房源状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_property_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:property:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:property:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:property:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:property:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="propertyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房源ID" align="center" prop="propertyId" />
      <el-table-column label="房源名称" align="center" prop="name" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="房东" align="center" prop="homeowner.userName" />
      <el-table-column label="户型" align="center" prop="layout">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_property_type" :value="scope.row.layout"/>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="合同名称" align="center">
        <template slot-scope="scope">
          <a :href="getFullFilePath(scope.row.contract.file)" target="_blank" style="color: #36d">{{ scope.row.contract.contractName }}</a>
        </template>
      </el-table-column>
      <el-table-column label="抵押规则" align="center" prop="depositRule">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_deposit_rule" :value="scope.row.depositRule"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="房源状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_property_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="其他相关信息" align="center" prop="otherInfo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:property:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:property:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房源信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="地址">
          <editor v-model="form.address" :min-height="192"/>
        </el-form-item>
        <el-form-item label="房源名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入房源名称" />
        </el-form-item>
        <el-form-item label="户型" prop="layout">
          <el-select v-model="form.layout" placeholder="请选择户型">
            <el-option
              v-for="dict in dict.type.sys_property_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="其他相关信息" prop="otherInfo">
          <el-input v-model="form.otherInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片" prop="photo">
          <image-upload v-model="form.photo"/>
        </el-form-item>
        <el-form-item label="抵押规则" prop="depositRule">
          <el-select v-model="form.depositRule" placeholder="请选择抵押规则">
            <el-option
              v-for="dict in dict.type.sys_deposit_rule"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template>
  <el-table v-loading="loading" :data="contractList" @row-click="handleRowClick" style="width: 100%">
    <el-table-column label="选择" width="55" align="center">
      <template slot-scope="scope">
        <el-radio v-model="selectedContractId" :label="scope.row.contractId"></el-radio>
      </template>
    </el-table-column>
    <el-table-column label="合同名称" align="center">
      <template slot-scope="scope">
        <a :href="getFullFilePath(scope.row.file)" target="_blank" style="color: #36d">{{ scope.row.contractName }}</a>
      </template>
    </el-table-column>
  </el-table>
</template>



      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProperty, getProperty, delProperty, addProperty, updateProperty } from "@/api/system/property";
import { listContract } from "@/api/system/contract";
import dict from "@/utils/dict";

export default {
  name: "Property",
  dicts: ['sys_property_status', 'sys_deposit_rule', 'sys_property_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房源信息表格数据
      propertyList: [],
      //日期范围
      dateRange: [],
      //合同
      //contract: [],
      // 合同管理表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      selectedContractId: '', // 选中的合同id
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        address: null,
        layout: null,
        price: null,
        creatTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dict,
    /** 查询房源信息列表 */
    getList() {
      this.loading = true;
      listProperty(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.propertyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询合同管理列表 */
    getContractList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        // 过滤掉delFlag为0的数据
        this.contractList = response.rows.filter(row => row.delFlag == '0');
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //补全文件路径
    getFullFilePath(filePath) {
      return `/dev-api${filePath}`;
    },
    getFileName(filePath) {
      // 截取文件名部分
      return filePath.split('/').pop();
    },
    // 表单重置
    reset() {
      this.form = {
        propertyId: null,
        address: null,
        layout: null,
        price: null,
        otherInfo: null,
        delFlag: null,
        photo: null,
        status: "0",
        deposit: null,
        depositRule: null,
        contractId: null,
      };
      this.resetForm("form");
    },
    resetContract(){
      this.form.contract = {
        contractId: null,
        contractName: null,
        file: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.propertyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChangeContract(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.selectedContractId = '';
      this.reset();
      this.resetContract();
      this.getContractList();
      this.open = true;
      this.title = "添加房源信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getContractList();
      const propertyId = row.propertyId || this.ids
      getProperty(propertyId).then(response => {
        this.form = response.data;
        console.log("11111111"+this.form.contract.contractName);
        this.open = true;
        this.title = "修改房源信息";
         // 如果 contractId 不为空，则将其设置为 selectedContractId 的值
        if (this.form.contract.contractId) {
          this.selectedContractId = this.form.contract.contractId;
        }
      });
    },
    handleRowClick(row) {
  this.selectedContractId = row.contractId;
  console.log("----------------" + row.contractId);
},

/** 提交按钮 */
submitForm() {
  // 将选项数据添加到表单数据中
  this.form.contractId = this.selectedContractId;

  this.$refs["form"].validate(valid => {
    if (valid) {
      if (this.form.propertyId != null) {
        console.log(this.form + "biaodanshuj1");
        updateProperty(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addProperty(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    }
  });
},
    /** 删除按钮操作 */
    handleDelete(row) {
      const propertyIds = row.propertyId || this.ids;
      this.$modal.confirm('是否确认删除房源信息编号为"' + propertyIds + '"的数据项？').then(function() {
        return delProperty(propertyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/property/export', {
        ...this.queryParams
      }, `property_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
